package jpb.module.service.department;

import jpb.module.dao.department.DepartmentDao;
import jpb.module.dao.member.MemberDao;
import jpb.module.daomain.department.Department;
import jpb.module.daomain.member.Member;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by haol on 2017/4/5.
 */
@Service
public class DepartmentService {
        @Autowired
        private DepartmentDao departmentDao;
        @Autowired
        private MemberDao memberDao;

        private Logger logger = LoggerFactory.getLogger(DepartmentService.class);

        public void add(Department department) {
                if (!checkIsExist(department.getOid(), department.getParentId())) {
                       department.setParentId(null);
                }
                departmentDao.add(department);
        }

        public void update(Department department) {
                departmentDao.update(department);
        }

        public void updateLogo(Department department) {
                departmentDao.updateLogo(department);
        }

        public void delete(Integer id) {
                List<Integer> dids = this.getAllSonId(id);
                for (Integer did : dids) {
                        departmentDao.delete(did);
                }
        }

        public Department get(Integer id) {
                return departmentDao.get(id);
        }

        public List<Department> getByOid(String oid) {
                return departmentDao.getByOid(oid);
        }





        public List<Member> getMem(Integer id) {
                List<Member> members = memberDao.getByDid(id);

//                List<Member> members = new ArrayList<Member>();
//                for (LoadMem loadMem : loadMems) {
//                        members.add(loadMem);
//                }
                return members;
        }



        public List<Department> getTree(String oid) {
                List<Department> departments = departmentDao.getByOid(oid);
                HashMap<Integer, Department> hashMap = new HashMap<Integer, Department>();
                for (Department department : departments) {
                        hashMap.put(department.getId(), department);
                }
                return this.constructTree(hashMap);
        }


        public List<Integer> getAllSonId(Integer id) {
                List<Integer> dids = departmentDao.getSons(id);
                for (Integer did : dids) {
                        List<Integer> dids0 = departmentDao.getSons(did);
                        for (Integer did1 : dids0) {
                                dids.add(did1);
                        }
                }
                dids.add(id);
                return dids;
        }

        public boolean checkIsExist(String oid, Integer pid) {
                return departmentDao.get(oid, pid) != null;
        }
        public boolean checkIsLeaf(Integer did) {
                int son = departmentDao.getSon(did).size();
                if (son == 0) {
                        return true;
                } else {
                        return false;
                }
        }
        private List<Department> constructTree(HashMap<Integer, Department> hashMap) {
                for (Map.Entry<Integer, Department> entry : hashMap.entrySet()) {
                        Department department = entry.getValue();
                        Integer prentId = department.getParentId();
                        if (prentId != null) {
                                hashMap.get(prentId).add(department);
                        }
                }
                List<Department> departments = new ArrayList<Department>();
                for (Map.Entry<Integer, Department> entry : hashMap.entrySet()) {
                        Department department = entry.getValue();
                        Integer prentId = department.getParentId();
                        if (prentId == null) {
                                department.setIcon("icon-th");
                                departments.add(department);
                        }
                }
                return departments;
        }

        public List<Department> getLeafByOid(String oid) {
                return departmentDao.getLeaf(oid);
        }


        public Department getByOidAndDname(String oid, String name) {
                return departmentDao.getByOidAndDname(oid, name);
        }

        public Department getByOidAndNum(String oid, String memberNum) {
                return departmentDao.getByOidAndNum(oid, memberNum);
        }
}
